﻿// <copyright file="Program.cs" company="Sinclair Community College">
// Copyright (c) Sinclair Community College. All rights reserved.
// </copyright>

namespace AppVFileFormat
{
    using System;
    using SinclairCC.AppV;

    /// <summary>
    /// This class contains the main entry point for the application.
    /// </summary>
    public class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        public static void Main()
        {
            // Record the current console foreground color.
            ConsoleColor defaultColor = Console.ForegroundColor;
            Console.ForegroundColor = ConsoleColor.White;

            DateTime start = DateTime.Now;

            ParseFolder(System.IO.Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "PKG files"));

            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("Process completed in {0}.", DateTime.Now.Subtract(start));

            // A little prompt-and-hold-the-screen action.
            Console.Write("\n\nPress <ENTER> to end this madness.");
            Console.ReadLine();

            // Set the console foreground color back to what it was when the program started.
            Console.ForegroundColor = defaultColor;
        }

        /// <summary>
        /// Processes the .SFT files in the specified folder and its subfolders.
        /// </summary>
        /// <param name="folderPath">The path of the folder containing the .SFT files to be processed.</param>
        private static void ParseFolder(string folderPath)
        {
            if (!System.IO.Directory.Exists(folderPath))
            {
                return;
            }

            System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(folderPath);

            if ((dir.Attributes & System.IO.FileAttributes.ReparsePoint) != System.IO.FileAttributes.ReparsePoint)
            {
                foreach (System.IO.DirectoryInfo di in dir.GetDirectories())
                {
                    ParseFolder(di.FullName);
                }

                foreach (System.IO.FileInfo fi in dir.GetFiles("*.pkg", System.IO.SearchOption.TopDirectoryOnly))
                {
                    if (fi.Exists)
                    {
                        try
                        {
                            Console.Write("processing ");
                            ConsoleColor currentColor = Console.ForegroundColor;
                            Console.ForegroundColor = ConsoleColor.Magenta;
                            Console.Write(fi.FullName);
                            Console.ForegroundColor = currentColor;
                            Console.WriteLine("...");
                            Console.WriteLine("File is {0:N0} bytes ({1:N2} MB).", fi.Length, fi.Length / Math.Pow(2, 20));
                            Console.WriteLine();

                            LoadVolumeFile(fi.FullName);

                            /*
                            Console.WriteLine();
                            Console.WriteLine();
                            */

                            /*
                            Console.Write("Press <enter> to continue...");
                            Console.ReadLine();
                            Console.Clear();
                            */
                        }
                        catch (System.IO.FileNotFoundException fnfError)
                        {
                            Console.WriteLine(fnfError.Message);
                            Console.WriteLine(fnfError.FileName);
                        }
                    }
                }
            }
        }

        /// <summary>
        /// Loads a volume file.
        /// </summary>
        /// <param name="filePath">
        /// The path of the volume file to load.
        /// </param>
        private static void LoadVolumeFile(string filePath)
        {
            VolumeFile vol = new VolumeFile(filePath);
            vol.Dispose();
        }
    }
}
